<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Appcont extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('date');
		$this->load->model('app_model');
		header('Content-Type: text/html; charset=UTF-8');//utf 8 cho trang web
	}
	protected $name = 'appcont.php';
	public function runGetData(){
		$this->getcontent(); //vnespress // nhung thong tin khoa hoc tren trang vnespress
		$this->getKDBank();	//vnespress //nhung thong tin kinh doanh nhan hang tren vnespress
		$this->getKDW();	//vnespress //nhung thong tin kinh doanh cua the gioi tren vnespress
		$this->getSieuThiSo();//pcword //nhung san pham cong nghe moi
		$this->getCntt();//tinh te //nhung cong nghe moi
	}
	public function index_test(){

		$url = 'http://vnexpress.net/rss/khoa-hoc.rss';
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		//$dataxml = $xmlDoc->saveXML();
		//$x = $xmlDoc->documentElement;
		$datatmp = $this->domnode_to_array($xmlDoc->documentElement);
		$data['khoahoc'] = $datatmp['channel']['item'];
		$this->load->view('html/homev',$data);
	}
	public function index(){
		$today = date('Y-m-d',now());
		$data['khoahoc'] = $this->app_model->getNewsByDay($today);
		$this->load->view('html/homev',$data);
	}
	public function getNewsDay($day=null){
//		$today = date('Y-m-d',now());
		$data['khoahoc'] = $this->app_model->getNewsByDay($day);
		$this->load->view('html/homev',$data);
	}
	private function getcontent(){
		
		$url = 'http://vnexpress.net/rss/khoa-hoc.rss';
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$datatmp = $this->domnode_to_array($xmlDoc->documentElement);
		$data = $datatmp['channel']['item'];
		$limit = count($data);
		$insert = array();
		for ($i=0;$i<$limit;$i++){
			$str = $data[$i]['description'];
			$start = strpos($str,'<img');
			$end = strpos($str,'></a');
			$img = substr($str, $start,$end-$start+1);

			$start = strpos($str,'<a');
			$end = strpos($str,'</a></br>');
			$a = substr($str, $start,$end-$start+9);
			$descip = str_replace($a, '', $str);
			
			$tmp = explode(',', $data[$i]['pubDate']);
			$tmp = explode('+', $tmp[1]);
			
			$insert[$i]['app_title'] = $data[$i]['title'];
			$insert[$i]['app_description'] = $descip;
			$insert[$i]['app_pubdate'] = date('Y-m-d H:i:s', strtotime($tmp[0]));
			$insert[$i]['app_link'] = $data[$i]['link'];
			$insert[$i]['app_guid'] = $data[$i]['guid'];
			$insert[$i]['app_img'] = $img;
			$insert[$i]['app_comments_id'] = '';
			$insert[$i]['app_details'] = $this->getDetailVNE($data[$i]['link']);
			$insert[$i]['app_delete_flag'] = 0;
			$insert[$i]['app_update'] = date('Y-m-d',now());
			$insert[$i]['type_id'] = 1;
			$this->app_model->saveNews($insert[$i]);
		}
	}
	public function showContent($id=null){
		
		$data = $this->app_model->getDetail($id);
//		$set['app_details'] = $data[0]['app_details'];
//		echo "<pre>************************************";
//		print_r($set);
//		echo "</pre>************************************";
//		die();
		$this->load->view('html/detailv',$data[0]);
	}
	private function parseDetailClass_old($url=''){//cu khong dung
		if (!empty($url)){
			$data='';
			$data = file_get_contents($url);
			$start = strpos($data,'<div class="cxtLeft">');
			$end = strpos($data,'<div class="fl dt-mid">');
			$detail = substr($data, $start,$end-$start);
			
			$start = strpos($detail,'<script');
			$end = strpos($detail,'</script>');
			$search = substr($detail, $start,$end-$start+9);
			$detail = str_replace($search, '', $detail);
			
			$end = strpos($detail,'<script>');
			$detail = substr($detail,0,$end);
			
			$end = strpos($detail,'<h1');
			$search = substr($detail,0,$end);
			$detail = str_replace($search, '', $detail);
			
			$start = strpos($detail,'<div class="reference_news">');
			$end = strpos($detail,'</div>');
			if ($start >0 && $end>0){
				$search = substr($detail, $start,$end-$start+6);
				$detail = str_replace($search, '', $detail);
			}
			
			$end = strpos($detail,'<a title');
			if ($end)
				$detail = substr($detail,0,$end);
			$detail = str_replace('@vnexpress.net','@congnghe.name.vn',$detail);
//			echo $detail;
			return $detail;
		}
		return false;
	}
	public function test(){//lay noi dung cua 1 calss div tu url

		//$url = "http://vnexpress.net/tin-tuc/khoa-hoc/doc-vi-10-hanh-vi-cua-con-nguoi-2861197.html";
		$ctx     = stream_context_create(array('http'=> array('timeout' => 10)));
		
		libxml_use_internal_errors(TRUE);
		$num = 0;
		if($html = @file_get_contents($url,false,$ctx)){
			$doc   = DOMDocument::loadHTML($html);
			$xpath = new DOMXPath($doc);
			foreach($xpath->query('//div[@class="fck_detail"]') as $div){
				//$data = $div->textContent;
//				$data = $div->firstChild->nodeValue;
				//$data = $div->nodeValue;
		 	}
		}else{
			echo "FAIL";
		}
		//echo $data;
		//$this->writeLog($this->name, '', $data);
	}
	
	public function getDetailVNE($url=null){//lay noi dung cua 1 calss div tu url

		//b1 tim den vi tri cua div class = fck_detail
		//b2 cat bo tu dau den vi tri nay
		//b3 tim tu vi tri the </div> xem co the mo div nao truoc khi den </div> khong dem xem co bao nhieu the mo va tim den dung so luong cac the div mo
		//b4 cat bo vi tri cuoi dung lay tu dau den the div dong
//		$url = "http://vnexpress.net/tin-tuc/khoa-hoc/doc-vi-10-hanh-vi-cua-con-nguoi-2861197.html";
		$url = htmlspecialchars_decode($url);
		if (!$url)
			return 'updating...';
		$data = file_get_contents($url);
		if (empty($data))
			return 'updating...';
		//b1
		$start = strpos($data,'<div class="fck_detail">');
		if ($start){//neu tim thay thi toi b2
			$detail = substr($data, $start);
			//b3
			$tmp = $detail;
			$limit = strpos($tmp, '</div>');
			$limitall = strlen($detail);
			$temdiv = 0;
			$temdivc = 0;
			$vitri = 4;
			for($i=0;$i<$limit;$i=$i+4){
				$vitri = strpos($tmp, '<div',$vitri+1);
				if ($vitri){
					$temdiv ++;
					$i = $vitri;
//					$tmp = substr($tmp, $vitri);
				}
			}
			if ($temdiv ==0){
				$x = $limitall - $limit-6;
			}else{
				for ($i =0 ;$i<=$temdiv;$i++){
					$temdivc = strpos($detail, '</div>',$temdivc+1);
				}
				$x = $limitall - $temdivc-6;
			}
			$detail = substr($detail, 0,-$x);
		}else {
			$detail = 'updating...';
		}
		$detail = str_replace('@vnexpress.net','@congnghe.name.vn',$detail);
//		echo $detail;
		return $detail;
		//echo $temdiv.$limit.'=='.$limitall;
	}
	public function getDetailICT($url=null){//lay noi dung cua 1 calss div tu url

		//b1 tim den vi tri cua div class = fck_detail
		//b2 cat bo tu dau den vi tri nay
		//b3 tim tu vi tri the </div> xem co the mo div nao truoc khi den </div> khong dem xem co bao nhieu the mo va tim den dung so luong cac the div mo
		//b4 cat bo vi tri cuoi dung lay tu dau den the div dong
//		$url = "http://ictnews.vn/home/Thu-thuat/9/Tao-anh-dong-GIF-tu-video-YouTube-tren-Chrome/111131/index.ict";
		$url = htmlspecialchars_decode($url);
		if (!$url)
			return 'updating...';
		$data = file_get_contents($url);
		if (empty($data))
			return 'updating...';
		//b1
		$start = strpos($data,'<div class="content-article ma">');
		if ($start){//neu tim thay thi toi b2
			$detail = substr($data, $start);
			//b3
			$tmp = $detail;
			$limit = strpos($tmp, '</div>');
			$limitall = strlen($detail);
			$temdiv = 0;
			$temdivc = 0;
			$vitri = 4;
			for($i=0;$i<$limit;$i=$i+4){
				$vitri = strpos($tmp, '<div',$vitri+1);
				if ($vitri){
					$temdiv ++;
					$i = $vitri;
//					$tmp = substr($tmp, $vitri);
				}
			}
			if ($temdiv ==0){
				$x = $limitall - $limit-6;
			}else{
				$tmp = substr($detail, $limit);
				for ($i =0 ;$i<=$temdiv;$i++){
					$temdivc = strpos($tmp, '</div>');
					$tmp = substr($tmp, $temdivc);
				}
				$x = $limitall - $temdivc-6;
			}
			$detail = substr($detail, 0,-$x);
		}else {
			$detail = 'updating...';
		}
//		$detail = str_replace('@vnexpress.net','@congnghe.name.vn',$detail);
//		echo $detail;
		return $detail;
		//echo $temdiv.$limit.'=='.$limitall;
	}
	public function getDetailPcw($url=null){//lay noi dung cua 1 calss div tu url

		//b1 tim den vi tri cua div class = fck_detail
		//b2 cat bo tu dau den vi tri nay
		//b3 tim tu vi tri the </div> xem co the mo div nao truoc khi den </div> khong dem xem co bao nhieu the mo va tim den dung so luong cac the div mo
		//b4 cat bo vi tri cuoi dung lay tu dau den the div dong
//		$url = "http://www.pcworld.com.vn/articles/preview/2013/07/1234158/may-chieu-giai-tri-3d-bang-may-chieu-tai-gia/";
		$url = htmlspecialchars_decode($url);
		if (!$url)
			return 'updating...';
		$data = file_get_contents($url);
		if (empty($data))
			return 'updating...';
		//b1
		$start = strpos($data,'ar-content-html');
		
		if ($start){//neu tim thay thi toi b2
			$detail = substr($data, $start-25);
			
			//b3
			$tmp = $detail;
			$limit = strpos($tmp, '</div>');
			$limitall = strlen($detail);
			$temdiv = 0;
			$temdivc = 0;
			$vitri = 4;
			
			for($i=0;$i<$limit;$i=$i+4){
				$vitri = strpos($tmp, '<div',$vitri+1);
				if ($vitri){
					$temdiv ++;
					$i = $vitri;
//					$tmp = substr($tmp, $vitri);
				}
			}
			if ($temdiv ==0){
				$x = $limitall - $limit-6;
			}else{
				for ($i =0 ;$i<=$temdiv;$i++){
					$temdivc = strpos($detail, '</div>',$temdivc+1);
				}
				$x = $limitall - $temdivc-6;
			}
			$detail = substr($detail, 0,-$x).'</div>';
		}else {
			$detail = 'updating...';
		}
		$detail = str_replace(array('src="/files/','href="/files/'),'src="http://www.pcworld.com.vn/files/',$detail);
//		echo $detail; die();
		return $detail;
		//echo $temdiv.$limit.'=='.$limitall;
	}
	protected function writeLog($fname, $line, $message) {
		$this->load->helper('file');
		$msg = "";
		$msg .= "[" . date("Y/m/d H:i:s", time()) . "] ";
		$msg .= $fname . " - ";
		$msg .= $line . " : ";
		$msg .= $message;
		$msg .= "\n";

		$path = 'C:/xampp/htdocs/congnghe/app_log/Applog.log';
		write_file($path, $msg);

		//chmod($path ."app_log/Applog.log", 0777);
	}
	public function getDetailTinhte($url=null){//lay noi dung cua 1 calss div tu url

		//b1 tim den vi tri cua div class = fck_detail
		//b2 cat bo tu dau den vi tri nay
		//b3 tim tu vi tri the </div> xem co the mo div nao truoc khi den </div> khong dem xem co bao nhieu the mo va tim den dung so luong cac the div mo
		//b4 cat bo vi tri cuoi dung lay tu dau den the div dong
//		$url = 'http://camera.tinhte.vn/threads/quy-trinh-lam-phim-hoat-hinh-3d-qua-bo-phim-%E2%80%9Cchuyen-tre-con%E2%80%9D.2151877/';
//		$url = 'http://camera.tinhte.vn/threads/quy-trinh-lam-phim-hoat-hinh-3d-qua-bo-phim-%E2%80%9Cchuyen-tre-con%E2%80%9D.2151877/';
		$url = htmlspecialchars_decode($url);
		if (!$url){
			return 'updating...';
		}
		$data = file_get_contents($url);
		if (empty($data)){
			return 'updating...';
		}
		//b1
		
		$start = strpos($data,'<div class="messageContent">');
		if ($start){//neu tim thay thi toi b2
			$detail = substr($data, $start);
			
			//b3
			$tmp = $detail;
			$limit = strpos($tmp, '<div class="facebookLike"');
			$limitall = strlen($detail);
			$temdiv = 0;
			$temdivc = 0;
			$vitri = 4;
			for($i=0;$i<$limit;$i=$i+4){
				$vitri = strpos($tmp, '<div',$vitri+1);
				if ($vitri){
					$temdiv ++;
					$i = $vitri;
					//$tmp = substr($tmp, $vitri);
				}
			}
			if ($temdiv ==0){
				$x = $limitall - $limit-6;
			}else{
				for ($i =0 ;$i<=$temdiv;$i++){
					$temdivc = strpos($detail, '</div>',$temdivc+1);
				}
				$x = $limitall - $temdivc-6;
			}
			//echo $x.'=='.$temdivc.'=='.$limitall;die();
			//$detail = substr($detail, 0,$temdivc).'</div>';
			$detail = substr($detail, 0,-$x).'</div>';
		}else {
			$detail = 'updating...';
		}
//		echo $detail;
		return $detail;
		//echo $temdiv.$limit.'=='.$limitall;
	}
	function domnode_to_array($node) {
		$output = array();
		switch ($node->nodeType) {
			case XML_CDATA_SECTION_NODE:
			case XML_TEXT_NODE:
				$output = trim($node->textContent);
				break;
			case XML_ELEMENT_NODE:
				for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
					$child = $node->childNodes->item($i);
					$v = $this->domnode_to_array($child);
					if(isset($child->tagName)) {
						$t = $child->tagName;
						if(!isset($output[$t])) {
							$output[$t] = array();
						}
						$output[$t][] = $v;
					}
					elseif($v) {
						$output = (string) $v;
					}
				}
				if(is_array($output)) {
					if($node->attributes->length) {
						$a = array();
						foreach($node->attributes as $attrName => $attrNode) {
							$a[$attrName] = (string) $attrNode->value;
						}
						$output['@attributes'] = $a;
					}
					foreach ($output as $t => $v) {
						if(is_array($v) && count($v)==1 && $t!='@attributes') {
							$output[$t] = $v[0];
						}
					}
				}
				break;
		}
		return $output;
	}
	public function getSieuThiSo(){//pcword
		$url = 'http://rss.pcworld.com.vn/articles/san-pham.rss';
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$datatmp = $this->domnode_to_array($xmlDoc->documentElement);
		$data = $datatmp['channel']['item'];
		$limit = count($data);
		$insert = array();
		for ($i=0;$i<$limit;$i++){
			$img='<img width="134" height="101" src="'.$data[$i]["media:thumbnail"]["@attributes"]["url"].'">';
			$insert[$i]['app_title'] = $data[$i]['title'];
			if (is_array($data[$i]['description']))
				$decrip = '';
			else 
				$decrip = $data[$i]['description'];
			$insert[$i]['app_description'] = $decrip;
			$insert[$i]['app_pubdate'] = date('Y-m-d H:i:s', strtotime($data[$i]['pubDate']));
			$insert[$i]['app_link'] = $data[$i]['link'];
			$insert[$i]['app_guid'] = $data[$i]['guid'];
			$insert[$i]['app_img'] = $img;//$data[$i]['media:thumbnail']['@attributes']['url'];
			$insert[$i]['app_comments_id'] = '';
			$insert[$i]['app_details'] = $this->getDetailPcw($data[$i]['link']);
			$insert[$i]['app_delete_flag'] = 0;
			$insert[$i]['app_update'] = date('Y-m-d',now());
			$insert[$i]['type_id'] = 3;
			$this->app_model->saveNews($insert[$i]);
		}
		
	}
	public function getKinhDoanh(){//ictnews
		$url = 'http://ictnews.vn/home/Handlers/RssHandler.ashx?ChannelID=8';
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$datatmp = $this->domnode_to_array($xmlDoc->documentElement);
		$data = $datatmp['channel']['item'];
		$limit = count($data);
	}
	public function getCntt(){//tinhte.vn
		$this->load->library('html2text');
		$url = 'http://feeds.feedburner.com/tinhte?format=xml';
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$datatmp = $this->domnode_to_array($xmlDoc->documentElement);
		$data = $datatmp['channel']['item'];
		$limit = count($data);
		
		$insert = array();
		for ($i=0;$i<$limit;$i++){
			$str = $data[$i]['description'];
			$this->html2text->html2text($str);
			$text = $this->html2text->get_text();
			$tmp = explode('Links:', $text);
			$descip = $tmp[0];
			$pattern = '/\[[0-9]+\]/';
			$descip = preg_replace($pattern, "", $descip);
			$start = strpos($str,'<img');
			$end = strpos($str,'</a');
			$img = substr($str, $start,$end-$start);

			$insert[$i]['app_title'] = $data[$i]['title'];
			$insert[$i]['app_description'] = $descip;
			$insert[$i]['app_pubdate'] = date('Y-m-d H:i:s', strtotime($data[$i]['pubDate']));
			$insert[$i]['app_link'] = $data[$i]['link'];
			$insert[$i]['app_guid'] = $data[$i]['guid'];
			$insert[$i]['app_img'] = $img;
			$insert[$i]['app_comments_id'] = '';
			$insert[$i]['app_details'] = $this->getDetailTinhte($data[$i]['guid']);
			$insert[$i]['app_delete_flag'] = 0;
			$insert[$i]['app_update'] = date('Y-m-d',now());
			$insert[$i]['type_id'] = 5;
			$this->app_model->saveNews($insert[$i]);
		}
			
	}
	public function getGiaiTri(){
		
	}
	private function getKDW(){
		$url = 'http://kinhdoanh.vnexpress.net/rss/quoc-te.rss';
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$datatmp = $this->domnode_to_array($xmlDoc->documentElement);
		$data = $datatmp['channel']['item'];
		$limit = count($data);
		
		$insert = array();
		for ($i=0;$i<$limit;$i++){
			$str = $data[$i]['description'];
			$start = strpos($str,'<img');
			$end = strpos($str,'></a');
			$img = substr($str, $start,$end-$start+1);
			
			$start = strpos($str,'<a');
			$end = strpos($str,'</a></br>');
			$a = substr($str, $start,$end-$start+9);
			$descip = str_replace($a, '', $str);
			
			$tmp = explode(',', $data[$i]['pubDate']);
			$tmp = explode('+', $tmp[1]);
			
			$insert[$i]['app_title'] = $data[$i]['title'];
			$insert[$i]['app_description'] = $descip;
			$insert[$i]['app_pubdate'] = date('Y-m-d H:i:s', strtotime($tmp[0]));
			$insert[$i]['app_link'] = $data[$i]['link'];
			$insert[$i]['app_guid'] = $data[$i]['guid'];
			$insert[$i]['app_img'] = $img;
			$insert[$i]['app_comments_id'] = '';
			$insert[$i]['app_details'] = $this->getDetailVNE($data[$i]['link']);
			$insert[$i]['app_delete_flag'] = 0;
			$insert[$i]['app_update'] = date('Y-m-d',now());
			$insert[$i]['type_id'] = 4;
			$this->app_model->saveNews($insert[$i]);
		}
	}
	private function getKDBank(){
		$url = 'http://kinhdoanh.vnexpress.net/rss/ngan-hang.rss';
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$datatmp = $this->domnode_to_array($xmlDoc->documentElement);
		$data = $datatmp['channel']['item'];
		$limit = count($data);
		$insert = array();
		for ($i=0;$i<$limit;$i++){
			$str = $data[$i]['description'];
			$start = strpos($str,'<img');
			$end = strpos($str,'></a');
			$img = substr($str, $start,$end-$start+1);

			$start = strpos($str,'<a');
			$end = strpos($str,'</a></br>');
			$a = substr($str, $start,$end-$start+9);
			$descip = str_replace($a, '', $str);
			
			$tmp = explode(',', $data[$i]['pubDate']);
			$tmp = explode('+', $tmp[1]);
			
			$insert[$i]['app_title'] = $data[$i]['title'];
			$insert[$i]['app_description'] = $descip;
			$insert[$i]['app_pubdate'] = date('Y-m-d H:i:s', strtotime($tmp[0]));
			$insert[$i]['app_link'] = $data[$i]['link'];
			$insert[$i]['app_guid'] = $data[$i]['guid'];
			$insert[$i]['app_img'] = $img;
			$insert[$i]['app_comments_id'] = '';
			$insert[$i]['app_details'] = $this->getDetailVNE($data[$i]['link']);
			$insert[$i]['app_delete_flag'] = 0;
			$insert[$i]['app_update'] = date('Y-m-d',now());
			$insert[$i]['type_id'] = 4;
			$this->app_model->saveNews($insert[$i]);
		}
	}
	private function parseDetailClass($url=''){//tam thoi khong can dung
//	public function parseDetailClass($url=''){
//		$url='http://kinhdoanh.vnexpress.net/photo/quoc-te/loi-ich-ty-dola-tu-golf-2860327.html';
		if (!empty($url)){
			$data='';
			$data = file_get_contents($url);
			$start = strpos($data,'<div class="fck_detail">');
			if (!$start){
				$start = strpos($data,'<div id="chitiet_anh">');
				$end = strpos($data,'</strong>');
				$detail = substr($data, $start,$end);
			}else{
				$end = strpos($data,'<div class="clear">');
				$detail = substr($data, $start,$end);
			}
			
			$end = strpos($detail,'</strong>');
			if ($end)
				$detail = substr($detail,0,$end+9);
			else{
				$end = strpos($detail,'<script type');
				if ($end)
					$detail = substr($detail,0,$end);
			} 
			$detail = str_replace('@vnexpress.net','@congnghe.name.vn',$detail);
			return $detail;
		}
		return false;
	}
	public function parseDetailClass_KDBank($url=''){//khong can dung
		$url='http://kinhdoanh.vnexpress.net/tin-tuc/ebank/ngan-hang/dau-thau-vang-thua-200-luong-2860901.html';
		if (!empty($url)){
			$data='';
			$data = file_get_contents($url);
			$start = strpos($data,'<div class="fck_detail">');
			if (!$start){
				$start = strpos($data,'<div id="chitiet_anh">');
				$end = strpos($data,'</strong>');
				$detail = substr($data, $start,$end);
			}else{
				$end = strpos($data,'<div class="clear">');
				$detail = substr($data, $start,$end);
			}
			
			
			$end = strpos($detail,'</strong>');
			if ($end)
				$detail = substr($detail,0,$end+9);
			
			$detail = str_replace('@vnexpress.net','@congnghe.name.vn',$detail);
//			echo $detail;die();
			return $detail;
		}
		return false;
	}
}